package com.ychen.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author ychen
 * @Date 21-3-8 下午3:05
 * @Version 1.0
 * Redis配置类
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        //Spring key序列器
        template.setKeySerializer(new StringRedisSerializer());
        //Spring value序列器
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //Hash key序列器
        template.setHashKeySerializer(new StringRedisSerializer());
        //Hash value序列器
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(connectionFactory);
        return template;
    }

}
